﻿using Microsoft.Office.Interop.Excel;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;
using ExcelRange = Microsoft.Office.Interop.Excel.Range; 

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class GetCellPostionNAction: BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            GetCellPostionNParam? param = item.ControlParam as GetCellPostionNParam;
            string searchObject = param.SearchObject;
            Workbook wb = this.GetObjVar<ExcelModel>(ref searchObject, globleData).WorkBookValue as Workbook;
            if(wb == null) { 
                throw new RpaRuntimeException(String.Format("Excel对象{0}不存在", searchObject));
            }

            ExcelUtil excelUtil = new ExcelUtil();
            excelUtil.SetApp(wb);

            string sheetName = param.SheetName;
            this.GetStringVar(ref sheetName, globleData);

            int searchRange = param.SearchRange;
            string rowString = param.Row;
            string columnString = param.Column;
            string endRowString = param.EndRow;
            string endColumnString = param.EndColumn;

            this.GetStringVar(ref rowString, globleData);
            this.GetStringVar(ref columnString, globleData);
            this.GetStringVar(ref endRowString, globleData);
            this.GetStringVar(ref endColumnString, globleData);

            int rowNum = excelUtil.GetRowNum(rowString, sheetName);
            int columnNum = excelUtil.GetColumnNum(columnString, sheetName);
            int endRowNum = excelUtil.GetRowNum(endRowString, sheetName);
            int endColumnNum = excelUtil.GetColumnNum(endColumnString, sheetName);
            
            string targetRowNum = param.TargetRowNum;
            string targetColumnNum = param.TargetColumnNum;
            this.GetStringVar(ref targetRowNum, globleData);
            this.GetStringVar(ref targetColumnNum, globleData);
            
            string searchValue = param.SearchValue;
            this.GetStringVar(ref searchValue, globleData);
            ExcelRange range = null;
            switch(searchRange) {
                case 0:
                    //区域
                    range = excelUtil.GetRange(sheetName, rowNum, columnNum, endRowNum, endColumnNum);
                    break;
                case 1:
                    //行
                    range = excelUtil.GetRange(sheetName, rowNum, 0, 0, 0);
                    break;
                case 2:
                    //列
                    range = excelUtil.GetRange(sheetName, 0, columnNum, 0, 0);
                    break;
                case 3:
                    //已使用区域
                    range = excelUtil.GetRange(sheetName, 0, 0, 0, 0);
                    break;
                default:
                    break;
            }
            int[] pos = excelUtil.GetCellPosition(searchValue, range);
            this.saveVar(globleData, targetRowNum, new IntModel(targetRowNum, pos[0]), DefaultContents.RUNNING_PARAM);
            this.saveVar(globleData, targetColumnNum, new IntModel(targetColumnNum, pos[1]), DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
